var datatable;// 定义一个全局的dataTable

$(function() {
	getIMList('init');
	$('#tableIM tbody').on('click', 'tr', function() {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			datatable.$('tr.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});
	
	var obj = document.getElementById('business_big_type_name');
	addEvent(obj,'change',function(datatable){
		var oTable = $('#tableIM').dataTable();  
		if(this.options[this.selectedIndex].value == '0'){
			
			oTable.fnSetColumnVis([25,26,27,28,29,30,31,32,33,34,35,26], false);//隐藏列  

			/*datatable.column(25).visible(false);
			datatable.column(26).visible(false);
			datatable.column(27).visible(false);
			datatable.column(28).visible(false);
			datatable.column(29).visible(false);
			datatable.column(30).visible(false);
			datatable.column(31).visible(false);
			datatable.column(32).visible(false);
			datatable.column(33).visible(false);
			datatable.column(34).visible(false);
			datatable.column(35).visible(false);
			datatable.column(36).visible(false);*/
		}else{
			oTable.fnSetColumnVis([25,26,27,28,29,30,31,32,33,34,35,26], true);
			/*datatable.column(25).visible(true);
			datatable.column(26).visible(true);
			datatable.column(27).visible(true);
			datatable.column(28).visible(true);
			datatable.column(29).visible(true);
			datatable.column(30).visible(true);
			datatable.column(31).visible(true);
			datatable.column(32).visible(true);
			datatable.column(33).visible(true);
			datatable.column(34).visible(true);
			datatable.column(35).visible(true);
			datatable.column(36).visible(true);*/
		}
	});
})

function queryAll(){
	var startTime=$("#beginDate").val();  
    var endTime=$("#endDate").val();  
    var userValue=$("#userValue").val();  
	if(startTime==null || startTime==""){
		layer.alert("开始时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(endTime==null || endTime==""){
		layer.alert("结束时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(!checkInterval()){
		layer.alert("时间相隔不能大于1天！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(userValue==null || userValue==""){
		layer.alert("手机号码,IMSI,IMEI或者MSIP不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	getIMList('query');
	var oTable = $('#tableIM').dataTable();  
	
	if($('#business_big_type_name option:selected').val() == '0'){
		oTable.fnSetColumnVis([25,26,27,28,29,30,31,32,33,34,35,36], false);
		/*datatable.column(25).visible(false);
		datatable.column(26).visible(false);
		datatable.column(27).visible(false);
		datatable.column(28).visible(false);
		datatable.column(29).visible(false);
		datatable.column(30).visible(false);
		datatable.column(31).visible(false);
		datatable.column(32).visible(false);
		datatable.column(33).visible(false);
		datatable.column(34).visible(false);
		datatable.column(35).visible(false);
		datatable.column(36).visible(false);*/
	}else{
		oTable.fnSetColumnVis([25,26,27,28,29,30,31,32,33,34,35,26], true);
		/*datatable.column(25).visible(true);
		datatable.column(26).visible(true);
		datatable.column(27).visible(true);
		datatable.column(28).visible(true);
		datatable.column(29).visible(true);
		datatable.column(30).visible(true);
		datatable.column(31).visible(true);
		datatable.column(32).visible(true);
		datatable.column(33).visible(true);
		datatable.column(34).visible(true);
		datatable.column(35).visible(true);
		datatable.column(36).visible(true);*/
	}
}


function getIMList(isInit) {
	var beginDate=$("#beginDate").val();
	var endDate=$("#endDate").val();
	var userType=$("#userType").val();
	var userValue=$("#userValue").val();
	var interface1 = $("#interface").val();
	var business_big_type_name = $("#business_big_type_name").val();
	
	$("#tableIM tbody tr").remove();
	if (datatable == undefined || datatable == "undefined" || datatable == null) {
	} else {
		datatable.destroy();
	}
	if(isInit == 'query'){
		layer.load(2);
	}
	
	datatable = $('#tableIM').DataTable({
		"aoColumnDefs" : [ {
	        sDefaultContent : '',
	        aTargets : [ '_all' ]
	    } ],
		"bPaginate" : true, // 翻页功能
		"bLengthChange" : false, // 改变每页显示数据数量
		"bFilter" : false, // 过滤功能
		"bSort" : true, // 排序功能
		"bInfo" : true,// 页脚信息
		"bAutoWidth" : true,// 自动宽度
		// "sPaginationType": "full_numbers",//显示分页的样式 还有simple_numbers等
		"sScrollX" : "100%",
		"sScrollXInner": "110%",
		"bScrollCollapse" : true,
		"oLanguage" : {
			"sLengthMenu" : "每页显示 _MENU_条",
			"sZeroRecords" : "没有找到符合条件的数据",
			"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
			"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
			"sInfoEmpty" : "没有记录",
			"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
			"sSearch" : "搜索：",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "前一页",
				"sNext" : "后一页",
				"sLast" : "尾页"
			}
		},
		"bProcessing" : true,
		"bServerSide" : true,
		"sAjaxSource" : "../../kpi/userOnlineDetails/getUserOnlineDetailInfo",
		"fnServerData" : function(sSource, aoData, fnCallback) {
			$.ajax({
				"type" : "post",
				"contentType" : "application/x-www-form-urlencoded",
				"url" : sSource,
				"dataType" : "json",
				"data" : {
					"aoData" : JSON.stringify(aoData),
					"beginDate" : beginDate,
					"endDate" : endDate,
					"userType" : userType,
					"userValue" : userValue,
					"interface" : interface1,
					"business_big_type_name"   : business_big_type_name,
					"isInit"     : isInit//由于要根据条件动态展示列表，故需要初始化列表，不查询数据即可
				}, // 以json格式传递
				"success" : function(resp) {
					if(resp.isInit){
					}else{
						fnCallback(resp.pageDate);
						layer.closeAll('loading');
						if(!resp.flag){
							alert("查询失败");
						}
					}
				}
			});
		},
		columns : [ {
			data : "start_date"
		}, {
			data : "end_date"
		}, {
			data : "msisdn"
		}, {
			data : "imsi"
		}, {
			data : "terminal_model"
		}, {
			data : "apn"
		}, {
			data : "rat"
		},{
			data : "city_code"
		}, {
			data : "enodeb_name"//基站地址太长，名称代替
		},{
			data : "business_type"
		},{
			data : "business_group"
		}, {
			data : "source_ip"
		},{
			data : "source_port"
		},{
			data : "destination_ip"
		},{
			data : "destination_port"
		},{
			data : "sgw_ip_addr" //SGW IP
		},{
			data : "pgw_ip_addr"//PGW IP
		},{
			data : "enodebid"
		},{
			data : "visited_plmn_id"
		},{
			data : "duration"/*,
			render : function(val,type,row){
				return val == null ? "" : +val+"ms";
			}*/
		},{
			data : "input_octets"
		},{
			data : "output_octets"
		},{
			data : "input_packet"//上行包数
		},{
			data : "output_packet" //下行包数
		},{
			data : "record_close_cause",
			render : function(val,type,row){
				if(val == '0'){
					return "session释放";
				}else if(val == '1'){
					return "业务应用结束";
				}else if(val == '2'){
					return "业务应用等待超时";
				}
			}
		},{
			data : "user_agent",
			"visible": false,
			render : function(val,type,row){
				var show = "";
				if(val.length >= 30){
					show = val.substring(0,30)+"..." 
					return "<label title='"+val+"' >"+show+"</label>";
				}
				return val;
			}
		},{
			data : "destination_url",
			"visible": false,
			render : function(val,type,row){
				var show = "";
				if(val.length >= 30){
					show = val.substring(0,30)+"..." 
					return "<label title='"+val+"' >"+show+"</label>";
				}
				return val;
			}
		},{
			data : "host",
			"visible": false
		},{
			data : "host",
			"visible": false
		},{
			data : "content_length",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "content_type",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "if_link",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "referer",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "http_action",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "http_status",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "resp_delay",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "behavior_target",
			"visible": false,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		}]
	});
}

function exportExcel() {
	var beginDate=$("#beginDate").val();
	var endDate=$("#endDate").val();
	var userType=$("#userType").val();
	var userValue=$("#userValue").val();
	var interface1 = $("#interface").val();
	var businessGigTypeName = $("#business_big_type_name").val();
	
	if(beginDate==null || beginDate==""){
		layer.alert("开始时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(endDate==null || endDate==""){
		layer.alert("结束时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(!checkInterval()){
		layer.alert("时间相隔不能大于1天！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(userValue==null || userValue==""){
		layer.alert("手机号码,IMSI,IMEI或者MSIP不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	
	layer.load(2);
	$.ajax({
		type : "POST",
		url : "../../kpi/userOnlineDetails/exportBigDataExcel?rand="+ Math.random(),
		dataType : "json",
		"data" : {
			"beginDate" : beginDate,
			"endDate" : endDate,
			"userType" : userType,
			"userValue" : userValue,
			"interface" : interface1,
			"businessGigTypeName" : businessGigTypeName
		},
		success : function(data) {
			layer.closeAll('loading');
			if (data.flag) {
				var url = "../../kpi/userOnlineDetails/downFiles?path="+ data.path;
				window.location.href = url;
			} else {
				alert(data.msgs);
			}
		}
	});
}
function addEvent(obj,type,fn){
	if(obj.addEventListener){
		obj.addEventListener(type,fn,false);
	}else if(obj.attachEvent){
		obj.attachEvent('on'+type,function(){
			fn.call(obj);
		});
	}
}

function resetForm(formId){
	$('#'+formId)[0].reset(); 
}

function checkEndTime(){  
    var startTime=$("#beginDate").val();  
    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
    var endTime=$("#endDate").val();  
    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
    if(end<start){  
        return false;  
    }  
    return true;  
} 

function checkInterval(){
	var startTime=$("#beginDate").val();  
    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
    var endTime=$("#endDate").val();  
    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
    
    var interval=end-start;
    var time=60*60*24*1000;
    if(interval>time){
    	return false;
    }
    return true;
}